package client_tree;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;

/**
 * Klasa koja predstavlja Tree model koji se koristi u ContactsTree stablu. Klasa je izvedena
 * iz DefaultTreeModel klase.
 * Klasa sadrzi odgovarajuce predefinisane metode DefaultTreeModela, koje omogucavaju
 * upravljanje modelom podataka.
 * 
 * @author UrosKrkic & Radivoje Popovic
 */
public class ContactsTreeModel extends DefaultTreeModel {

	
	/**
	 * Konstrutkor.
	 * Kreira novi ContactsTreeModel, sa root-om modela koji je instanca klase RootNode.
	 * @author UrosKrkic & Radivoje Popovic
	 */
	public ContactsTreeModel() {
		super(new RootNode("root"));
	}
	

	@Override
	public Object getChild(Object parent, int index) {
		if (parent instanceof User){
			return null;
		} else
		if (parent instanceof ClientModelNode){
			return ((ClientModelNode)parent).getChildAt(index);
		} else
		if (parent instanceof RootNode) {
			return ((RootNode)parent).getChildAt(index);
		}
		
		return getRoot(); 
	}

	
	@Override
	public int getChildCount(Object parent) {
		if (parent instanceof User) {
			return 0;
		} else
		if (parent instanceof ClientModelNode) {
			return ((ClientModelNode)parent).getChildCount();
		} else
		if (parent instanceof RootNode) {
			return ((RootNode)parent).getChildCount();
		} else
			return ((DefaultMutableTreeNode)parent).getChildCount();
	}

	
	@Override
	public int getIndexOfChild(Object parent, Object child) {
        if(parent == null)
            return -1;
		int ind = ((TreeNode)parent).getIndex((TreeNode)child); 

		return ind;
	}

	
	@Override
	public boolean isLeaf(Object node) {
		return (node instanceof User);
	}
	
}
